package cn.tqfeiyang.smartadmin.office.service;

import cn.tqfeiyang.smartadmin.office.entity.NoticeCategoryEntity;
import cn.tqfeiyang.smartadmin.office.mapper.NoticeCategoryMapper;
import cn.tqfeiyang.smartadmin.office.request.NoticeCategoryCreateDTO;
import cn.tqfeiyang.smartadmin.office.request.NoticeCategoryUpdateDTO;
import cn.tqfeiyang.smartadmin.office.request.NoticeCategoryQueryDTO;
import cn.tqfeiyang.smartadmin.office.response.NoticeCategoryQueryVO;
import cn.tqfeiyang.smartadmin.office.response.NoticeCategoryDetailVO;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.github.pagehelper.PageHelper;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import cn.tqfeiyang.smartadmin.commons.exception.BusinessException;
import cn.tqfeiyang.smartadmin.commons.pagination.PageData;
import cn.tqfeiyang.smartadmin.commons.toolbox.BeanUtils;
import cn.tqfeiyang.smartadmin.commons.toolbox.ValidationUtils;

import java.util.List;
import java.util.Set;

/**
 * 通知分类服务
 *
 * @author tqfeiyang
 * @since 2024-09-07
 */
@Service
public class NoticeCategoryService {

    /**
     * 通知分类映射接口
     */
    @Resource
    private NoticeCategoryMapper noticeCategoryMapper;

    /**
     * 分页查询通知分类
     *
     * @param dto 请求数据
     * @return 通知分类分页
     */
    public PageData<NoticeCategoryQueryVO> getNoticeCategoryPage(NoticeCategoryQueryDTO dto) {
        ValidationUtils.validateProperties(dto);
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<NoticeCategoryEntity> list = noticeCategoryMapper.selectList(dto.getWrapper());
        return PageData.create(list, NoticeCategoryQueryVO.class);
    }

    /**
     * 获取全部通知分类
     *
     * @return 通知分类列表
     */
    public List<NoticeCategoryQueryVO> getAllNoticeCategoryList() {
        List<NoticeCategoryEntity> list = noticeCategoryMapper.selectList(null);
        return BeanUtils.map(list, NoticeCategoryQueryVO.class);
    }

    /**
     * 获取通知分类详情
     *
     * @param categoryId 分类id
     * @return 通知分类详情
     */
    public NoticeCategoryDetailVO getNoticeCategoryInfo(Long categoryId) {
        NoticeCategoryEntity noticeCategoryEntity = checkNoticeCategoryExists(categoryId);
        return BeanUtils.map(noticeCategoryEntity, NoticeCategoryDetailVO.class);
    }

    /**
     * 添加通知分类
     *
     * @param dto 请求数据
     * @return 分类id
     */
    @Transactional(rollbackFor = Exception.class)
    public Long createNoticeCategory(NoticeCategoryCreateDTO dto) {
        ValidationUtils.validateProperties(dto);
        NoticeCategoryEntity noticeCategoryEntity = BeanUtils.copy(dto, NoticeCategoryEntity.class);
        noticeCategoryMapper.insert(noticeCategoryEntity);
        return noticeCategoryEntity.getCategoryId();
    }

    /**
     * 修改通知分类
     *
     * @param categoryId 分类id
     * @param dto 请求数据
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateNoticeCategory(Long categoryId, NoticeCategoryUpdateDTO dto) {
        checkNoticeCategoryExists(categoryId);
        ValidationUtils.validateProperties(dto);
        NoticeCategoryEntity noticeCategoryEntity = BeanUtils.copy(dto, NoticeCategoryEntity.class);
        noticeCategoryEntity.setCategoryId(categoryId);
        noticeCategoryMapper.updateById(noticeCategoryEntity);
    }

    /**
     * 删除通知分类
     *
     * @param categoryId 分类id
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteNoticeCategory(Long categoryId) {
        checkNoticeCategoryExists(categoryId);
        noticeCategoryMapper.deleteById(categoryId);
    }

    /**
     * 检查通知分类实体是否存在
     *
     * @param categoryId 分类id
     * @return 通知分类实体
     */
    protected NoticeCategoryEntity checkNoticeCategoryExists(Long categoryId) {
        Assert.notNull(categoryId, "分类id不能为空");
        NoticeCategoryEntity noticeCategoryEntity = noticeCategoryMapper.selectById(categoryId);
        if (ObjectUtils.isEmpty(noticeCategoryEntity)) {
            throw new BusinessException("通知分类不存在");
        }
        return noticeCategoryEntity;
    }

    /**
     * 批量删除通知分类
     *
     * @param categoryIds 分类id集合
     */
    @Transactional(rollbackFor = Exception.class)
    public void batchDeleteNoticeCategories(Set<Long> categoryIds) {
        Assert.notNull(categoryIds, "分类id集合不能为空");
        categoryIds.forEach(categoryId -> {
            checkNoticeCategoryExists(categoryId);
            noticeCategoryMapper.deleteById(categoryId);
        });
    }

    /**
     * 导出通知分类Excel
     *
     * @param response
     */
    public void exportNoticeCategoryExcel(HttpServletResponse response) {
        List<NoticeCategoryQueryVO> list = getAllNoticeCategoryList();
        //ExcelUtils.exportExcel(response, list, NoticeCategoryQueryVO.class, "通知分类");
    }

    /**
     * 修改排序
     *
     * @param categoryId 分类id
     * @param sort 排序
     */
    public void updateSortField(Long categoryId, Integer sort) {
        checkNoticeCategoryExists(categoryId);
        Assert.notNull(sort, "sort不能为空");
        NoticeCategoryEntity noticeCategoryEntity = new NoticeCategoryEntity();
        noticeCategoryEntity.setCategoryId(categoryId);
        noticeCategoryEntity.setSort(sort);
        noticeCategoryMapper.updateById(noticeCategoryEntity);
    }
}
